package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.TRolePermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 权限数据,服务层
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private TRolePermissionDao tRolePermissionDao;

    /**
     * 分页查询
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Permission> page = permissionDao.findPage(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增权限
     * @param permission
     * @return
     */
    @Override
    public boolean add(Permission permission) {
        int count = permissionDao.add(permission);
        return count > 0;
    }

    /**
     * find permission by id
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    /**
     * 编辑权限
     * @param permission
     * @return
     */
    @Override
    public boolean edit(Permission permission) {
        int count = permissionDao.edit(permission);
        return count > 0;
    }

    /**
     * 根据权限id删除权限
     * @param id
     * @return
     */
    @Override
    public boolean delete(Integer id) {
        //在角色-权限 中间表中删除角色-权限关系记录
        int rscount = tRolePermissionDao.deleteByPermissionId(id);
        //再在权限表中删除权限
        int count = permissionDao.deleteById(id);
        return count > 0;
    }
}
